/**
 * The view displays the game.
 * 
 * created 11-18
 * @author Jonathan Odom, Andrew Cook
 */
package vooga.core;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class VoogaView extends JFrame{
	
	public static final String GAMES_PACKAGE = "games";
	public static final String LEVEL_FACTORY_CLASS = "LevelFactory";

	protected Layer myBackground;
    protected Layer myForeground;
    protected LayerSet myLayers;
	private Game myCurrGame;
	private JMenuBar menuBar;
	protected Player myPlayer;
	//private JLabel statusBar;
	
	final static Dimension DEFAULT_DIMENSION = new Dimension(800, 600);
	final static String VOOGA_TITLE = "Vooga";
	
	public VoogaView()
	{
		super(VOOGA_TITLE);
		loadMenus();
		setSize(DEFAULT_DIMENSION);
		setVisible(true);
		
		setLayout(new GridLayout());
		add(myLayers = new LayerSet());
        myLayers.setSize(DEFAULT_DIMENSION);
        myLayers.setVisible(true);
addComponentListener(new ComponentAdapter()
        {
			public void componentResized(ComponentEvent e)
			{
				resizeLayers();
			}
        });
                       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        String s = promptForUser();
        myPlayer = new Player(s, new Status(s, ""));
	}
	
	/**
	 * automagically set up the game menu with game choices
	 * search in games package for games
	 *
	 */
	private void loadMenus()
	{
		menuBar = new JMenuBar();
		
		JMenu gameMenu = new JMenu("Games");
		menuBar.add(gameMenu);
	    
		//go through all the subpackages in the game packages
		java.io.File gamesFolder = new java.io.File(GAMES_PACKAGE);
		for(java.io.File gamePackage : gamesFolder.listFiles())
		{
			if(gamePackage.isDirectory())
				gameMenu.add(new GameMenuItem(gamePackage.getName(), this));
		}
		this.setJMenuBar(menuBar);
		
		menuBar.add(new JMenuItem(new PauseAction()));
	}
	
	public String promptForUser()
	{
		 JInternalFrame intFrame = new JInternalFrame();
         String input = (String)JOptionPane.showInputDialog(
                 intFrame,
                 "Please enter your name to begin playing",
                 "",
                 JOptionPane.PLAIN_MESSAGE,
                 null,
                 null,
                 null);
         return input;
	}
		
	/**
	 * reset the foreground and background layers
	 * 
	 */
	public void resetLayers()
	{
		myLayers.clear();
	}
	private void resizeLayers()
	{
		myLayers.setSize(getContentPane().getSize());
	}
	
	public void showError(String error)
	{
		javax.swing.JOptionPane.showMessageDialog(this, error);
	}
	
	protected class PauseAction extends AbstractAction
	{
		final static String PAUSE = "Pause";
		final static String UNPAUSE = "Unpause";
		boolean shouldPause;

		public PauseAction()
		{
			super(PAUSE);
			shouldPause = shouldPause();
//			if(myCurrGame == null)
//				setEnabled(false);
		}

		public void actionPerformed(ActionEvent e) {
			myCurrGame.getCurrLevel().setPaused(shouldPause);
			shouldPause = !shouldPause;
			setTitle(VOOGA_TITLE + (shouldPause ? "" : " (paused)"));
		}
		
		/**
		 * am I a pause or unpause action?
		 * @return
		 */
		public boolean shouldPause()
		{
			return myCurrGame == null || myCurrGame.myCurrLevel == null || !myCurrGame.myCurrLevel.isPaused();
		}
	}

	/**
	 * a game choice in the game menu
	 * @author congyiwu
	 *
	 */
	class GameMenuItem extends JMenuItem implements ActionListener
	{		
		protected String myGameName;
		protected VoogaView myParent;
		
		/**
		 * 
		 * @param parent reference to the parent VoogaView that gets passed to the levels
		 * @param gameFolder the folder that the game is stored in
		 */
		public GameMenuItem(String gameName, VoogaView parent)
		{
			super(gameName);
			myGameName = gameName;
			myParent = parent;
			addActionListener(this);
		}
		
		/**
		 * start the game when the menu item is clicked
		 */
		public void actionPerformed(ActionEvent e)
		{
				try
				{
					if(myCurrGame != null && myCurrGame.getCurrLevel() != null)
						myCurrGame.getCurrLevel().quitLevel();
					
					myCurrGame = new Game(myParent, Class.forName(GAMES_PACKAGE + "." + myGameName + "." + LEVEL_FACTORY_CLASS));
				}
				catch (Exception e1)
				{
					e1.printStackTrace();
					showError(Game.BROKEN_GAME_ERROR);
					return;
				}
				//for now all games just use a single default player with 3 lives
				myPlayer.getStatus().setGameName(myGameName);
				myPlayer.getStatus().initStatus();
				myCurrGame.nextLevel(myPlayer);
		}
	}

	public LayerSet getLayerSet() {
		return  myLayers;
	}
}